import Utils from "../../Utils";
import { GamePropType } from "../info/GameEnum";
import { Global } from "../info/Global";
import PlayerManager from "../manager/PlayerManager";
import Dialog from "./Dialog";

const { ccclass, property } = cc._decorator;

const overTag = [
    ["巅峰之上，风景独好！", "荣耀加冕，不负所望！", "巅峰时刻，荣耀永存！", "王者之路，始于足下！", "勇者无敌，胜券在握！", "冠军之光，照亮前路！", "实力见证，荣耀巅峰！", "勇者无畏，战无不胜！", "巅峰对决，我主沉浮！"],
    ["虽败犹荣，继续努力！", "实力见证，再攀高峰！", "强者之路，永不止步！", "虽未夺冠，风采依旧！", "实力不凡，未来可期！", "亚军风采，值得骄傲！", "继续努力，再攀高峰！", "强者对决，虽败犹荣！", "亚军之光，同样耀眼！"],
    ["季军风采，继续前行！", "突破自我，永不止步！", "季军荣耀，同样闪耀！", "勇往直前，再创辉煌！", "突破自我，赢得尊重！", "季军之路，同样精彩！", "拼搏进取，未来无限！", "季军风采，永不言败！"],
    ["紧追不舍，未来可期！", "积累经验，再创佳绩！", "虽未进三，实力可嘉！", "积累经验，迎接挑战！", "勇往直前，不畏艰难！", "挑战自我，未来可期！", "实力见证，潜力无限！", "永不言弃，终会成功！"],
    ["百尺竿头，更进一步！", "虽未登顶，永不言败！", "虽未登顶，继续努力！", "不断前行，再创新高！", "实力不凡，未来可期！", "永不言败，永不止步！", "虽败犹荣，勇者无畏！", "勇者之路，再创佳绩！"],
    ["吸取教训，下次更好！", "永不放弃，终会成功！", "勇往直前，无畏挑战！", "永不言弃，终创佳绩！", "积累经验，迎接挑战！", "挑战自我，终创辉煌！", "永不言弃，终会崛起！", "虽败犹荣，继续前行！"]
]

@ccclass
export default class GameOverDialog extends Dialog {

    @property(cc.SpriteFrame)
    selfRankFrame: cc.SpriteFrame = null;

    @property(cc.Node)
    rankNode: cc.Node = null;

    @property(cc.Label)
    tip: cc.Label = null

    @property(cc.Label)
    coinLabel: cc.Label = null

    @property(cc.Label)
    boxLabel: cc.Label = null

    @property(cc.Node)
    light: cc.Node = null

    public init() {
        cc.tween(this.light)
            .to(5, { angle: 360 })
            .set({ angle: 0 })
            .union()
            .repeatForever()
            .start();

        let deathRank = Global.currentGameData.deathRank.concat();

        if (!deathRank.includes(PlayerManager.instance.controllerPlayer)) {
            deathRank.push(PlayerManager.instance.controllerPlayer);
        }

        deathRank.reverse();

        let gameBouns: { coin: any; box: any; };
        if (Global.chooseMode == 0) {
            while (deathRank.length < this.rankNode.children.length) {
                deathRank.unshift(null);
            }
            let rank = deathRank.indexOf(PlayerManager.instance.controllerPlayer);
            this.tip.string = Utils.getRandomChoose(overTag[rank]);
            gameBouns = Global.GameBouns[rank];
            this.coinLabel.string = "+" + gameBouns.coin;
            this.boxLabel.string = "+" + gameBouns.box;

            for (let i = 0; i < this.rankNode.children.length; i++) {
                const rNode = this.rankNode.children[i];
                if (deathRank[i]) { 
                    if (deathRank[i] === PlayerManager.instance.controllerPlayer) {
                        rNode.getComponent(cc.Sprite).spriteFrame = this.selfRankFrame;
                        rNode.children[0].color = cc.color(0, 255, 0);
                        rNode.children[1].color = cc.color(0, 255, 0);
                        rNode.children[1].getComponent(cc.Label).string = "本机玩家";
                    } else {
                        rNode.children[1].getComponent(cc.Label).string = deathRank[i].playerName;
                    }
                } else {
                    rNode.children[1].getComponent(cc.Label).string = "???";
                }
            }
        } else if (Global.chooseMode == 1) {
            while (deathRank.length < 2) {
                deathRank.unshift(null);
            }
            this.rankNode.active = false;
            this.tip.node.y = -30;
            this.tip.node.x = 15;
            if (deathRank[0] === PlayerManager.instance.controllerPlayer) {
                this.tip.string = "胜利！";
                gameBouns = Global.GameBouns[2];
            } else {
                this.tip.string = "失败！";
                gameBouns = Global.GameBouns[5];
            }

            this.coinLabel.string = "+" + gameBouns.coin;
            this.boxLabel.string = "+" + gameBouns.box;
        }
        Global.gameInfo.coin += gameBouns.coin;
        Global.gameInfo.propCount[GamePropType.魔盒] += gameBouns.box;
        Global.saveGameInfo();
    }

    onClick(event: cc.Event) {
        let name = event.target.name;
        switch (name) {
            case "领取":
                cc.director.loadScene("Main");
                break;
        }
    }
}
